package com.yangle.frame.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yangle.frame.admin.entity.ResultStatus;
import com.yangle.frame.admin.entity.User;
import com.yangle.frame.admin.service.UserService;
import com.yangle.frame.common.utils.GenerateCodingUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.util.StringUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangle on 2018/6/1.
 */
@Controller
@RequestMapping("/frame")
public class GenerateController {
    @Value("${root.path}")
    public String root_path;
    @RequestMapping("/codingPage")
    public String codingPage(String tableName, String sqlTableName, ModelMap modelMap){
        modelMap.put("tableName",tableName);
        modelMap.put("sqlTableName",sqlTableName);
       return "frame/codingpage";
    }
@RequestMapping("/coding")
    public String coding(HttpServletRequest request){
    String modelName = request.getParameter("modelName");
    String sqlTableName = request.getParameter("sqlTableName");
    String tableName = request.getParameter("tableName");
    String author = request.getParameter("author");
    String scope = request.getParameter("scope");
    String generatePath = request.getParameter("generatePath");
    Map<String, String> map=new HashMap<>();
    map.put("package_name","com.yangle.frame."+modelName);
    map.put("model_name",modelName);
    map.put("author",author);
    map.put("scope",scope);
    if(StringUtil.isNotEmpty(generatePath)){
        map.put("generate_path",generatePath);
    }else{
        map.put("generate_path",root_path);

    }


    map.put("table_name",sqlTableName);
    map.put("table_zh_name",tableName);
    map.put("root_path",root_path);
    try {
        GenerateCodingUtil.generate(map);
    } catch (IOException e) {
        e.printStackTrace();
    }
   request.setAttribute("result",new ResultStatus(true,"代码已生成"));
        return "common/result";
}
}
